package com.taing.sample.web;

import cn.hutool.core.io.FileUtil;
import org.apache.tomcat.util.buf.HexUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

/**
 * @Author tianyi
 * @Description // todo
 * @Date 2024 - 12 - 29
 **/
public class SearchUitl {
    /**
     * @param path      文件全路径
     * @param algorithm 算法 MD5, SHA-1, SHA-256
     * @return checksum
     * @throws NoSuchAlgorithmException
     * @throws IOException
     */
    public static String extractCheckSum(String path, String algorithm) throws Exception {
        if (!FileUtil.isFile(path)) {
            throw new Exception("no such file");
        }
        MessageDigest digest = MessageDigest.getInstance(algorithm);
        byte[] fileBytes = Files.readAllBytes(Paths.get(path));
        digest.update(fileBytes);
        byte[] digested = digest.digest();
        return HexUtils.toHexString(digested);
    }

    static final String source_path = "/Users/timwu/tt/sourcefile";
    static final String index_path = "/Users/timwu/tt/index";

    public static boolean isEmptyIndexDir(String paht) {
        File file = new File(paht);
        if (file.isDirectory()) {
            if (Arrays.stream(file.list()).filter(a -> a.contains("segments")).count() > 0) {
                return false;
            }
        }
        return true;
    }

    public static enum FullFileField {
        filename, content, path, size, filecode
    }

}
